<?php
$post=json_decode($this->input->raw_input_stream,true);


function fullpath($url)
{
    if(substr($url,0,2)=="//")
    {
        return "http:".$url;
    }
    else
    {
        return $url;
    }
}
$data = $post["json"];
$gid=$data["id"];
if($this->goods_model->getbytbid($gid,$shop["id"])) die("exist");

$product=array(
    "catid"=>$shop["id"],
    "tbid"=>$gid,
);
$maxprice=0;
foreach($data['variants'] as $v)
{
    if($v["price"]>$maxprice)
    {
        $maxprice=$v['price'];
    }
}

if($maxprice==0)
{
    die("no price find");
}

$body=preg_replace("#<a[^>]*>(.*?)</a>#is", "",$data["body_html"]);
$body=str_replace(["Highly Recommend:"],"",$body);

$product["desc"] = $body;

if(!isset($data['images']) || !$data['images'])
{
    die("skip no image goods");
}
$product["thumb"]=[];

foreach ($data['images'] as $k => $v) 
{
    if($v["variant_ids"])
    {
        continue;
    }
    if(strstr($v['src'],"?"))
    {
        $tmp=explode("?",$v['src']);
        $v['src']=$tmp[0];
    }
    $product["thumb"][] = $v['src'];
}

if(isset($data["tags"]) && $data["tags"])
{
    $product["tags"]=implode(",",$data['tags']);   
}
$product["thumb"]=serialize($product["thumb"]);        
$product["name"]=$data['title'];
$product["price"]=sprintf("%.2f",$maxprice);
if($data["product_type"])
{
    $product["product_type"]=$data["product_type"];
}
$prop=array();
if($data["options"]){
    if(count($data["options"])==1 && $data["options"][0]["name"]=="Title" && count($data["options"][0]["values"])==1 && $data["options"][0]["values"][0]=="Default Title")
    {   

    }
    else
    {
        foreach($data["options"] as $ccc=>$vs){
            if(!isset($vs["values"]))
            {
                continue;
            }
            if($vs["name"]=="Title" && count($vs["values"])==1 && $vs["values"][0]=="Default Title")
            {
                continue;
            }
            $v1=array("name"=>$vs["name"],"image"=>[]);
            $priceonval = [];
            foreach($vs["values"] as $vc){
                $v1["value"][]=$vc;   
                if($ccc==0)
                {
                    foreach($data['variants'] as $vv){
                        if($vv["featured_image"] && $vv["option".$vs["position"]]==$vc)
                        {
                            $vpic=$vv["featured_image"]["src"];
                            if(strstr($vpic,"?"))
                            {
                                $tmp=explode("?",$vpic);
                                $vpic=$tmp[0];
                            }
                            //if(!in_array($vpic,$v1["image"]))
                            //{
                                $v1["image"][]=$vpic;
                            //}                            
                        }

                        if($vv["option".$vs["position"]]==$vc && !in_array($vc,$priceonval))
                        {
                            $priceonval[]=$vc;
                            $v1["price"][]=$vv['price'];
                        }
                    }
                }           
            }        
            $prop["vv"][]=$v1;
        }
    
        foreach($data['variants'] as $vv){
            $prop["vc"][]=sprintf("%.2f",$vv['price']);
        }
    }

}

$product["name"]=ucwords(strtolower($product["name"]));

if($post["fetchdesc"]==0)
{
    $product["desc"]="";
}
if(isset($post["cat"]))
{
    $product["collection"]=$post["cat"];
}
$product["option"]=$prop;
$product["source"]="shopify";
$product["source_url"]=$post["domain"]."/products/".$data["handle"];
$product["option"]=serialize($prop);
$product["meta"]=$product["name"];

$newproduct=$this->goods_model->create($product);
if($newproduct){
    die("ok");
}else{
    die("error");
}
